import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/storage'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '@/views/login/login.vue')
  },
  {
    path: '/',
    name: 'layout',
    redirect: 'welcome',
    component: () => import(/* webpackChunkName: "about" */ '@/views/layout/layout.vue'),
    children: [
      {
        path: '/welcome',
        name: 'welcome',
        component: () => import(/* webpackChunkName: "about" */ '@/views/welcome/welcome.vue')
      },
      {
        path: '/users',
        name: 'users',
        component: () => import(/* webpackChunkName: "about" */ '@/views/users/users.vue')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import(/* webpackChunkName: "about" */ '@/views/root/roles.vue')
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import(/* webpackChunkName: "about" */ '@/views/root/rights.vue')
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import(/* webpackChunkName: "about" */ '@/views/things/goods.vue')
      },
      {
        path: '/goods/add',
        name: 'add',
        component: () => import(/* webpackChunkName: "about" */ '@/views/things/add.vue')
      },
      {
        path: '/params',
        name: 'params',
        component: () => import(/* webpackChunkName: "about" */ '@/views/things/params.vue')
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import(/* webpackChunkName: "about" */ '@/views/things/categories.vue')
      },
      {
        path: '/orders',
        name: 'orders',
        component: () => import(/* webpackChunkName: "about" */ '@/views/orders/orders.vue')
      },
      {
        path: '/reports',
        name: 'reports',
        component: () => import(/* webpackChunkName: "about" */ '@/views/reports/reports.vue')
      },
    ]
  },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  routes
})

const whiteList = ['/login']
/* 路由导航守卫 */
router.beforeEach((to, from, next) => {
  /*
  1-获取token
  2、根据token判断
    有token
      表示已经登录了，可以直接访问，需要放行 next
    无token
      判断是不是有的页面没有token也可以访问  比如 /login  【白名单】  是可以直接访问的
      判断没有token又不能直接去访问的【不是白名单的】  跳转到登录页面
  
  */
  const token = getToken()
  if (token) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
